package com.yilin.tms.gps.controller;

import com.google.gson.reflect.TypeToken;
import com.yilin.commons.exception.BusinessException;
import com.yilin.commons.util.ArrayUtil;
import com.yilin.commons.util.JsonUtil;
import com.yilin.tms.core.application.ReturnData;
import com.yilin.tms.core.commons.annotation.NotNull;
import com.yilin.tms.core.commons.annotation.SkipLoginVerify;
import com.yilin.tms.core.commons.base.BaseController;
import com.yilin.tms.core.entity.gps.Location;
import com.yilin.tms.core.entity.gps.hyb.*;
import com.yilin.tms.gps.service.IGpsHybService;
import org.checkerframework.checker.units.qual.A;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.*;


@RestController
@RequestMapping("/gps/hyb/")
public class GpsHybController extends BaseController {

    @Resource
    IGpsHybService gpsHybService;

//    /**
//     * 根据北斗定位时间查询线路
//     * 【轨迹·付费】
//     */
//    @PostMapping("gpsDataWithTruckNoWithTime")
//    @NotNull({"vehicleNo", "startTime","endTime","vco"})
//    public ReturnData<Object> gpsDataWithTruckNoWithTime(@Explain("车牌号")String vehicleNo,@Explain("运单号")String transportNo, @Explain("开始时间")Date startTime, @Explain("结束时间")Date endTime) {
//        List<Location> gpsDataWithTruckNoWithTime = gpsHybService.getTack(vehicleNo,transportNo,startTime,endTime);
//        return jsonView(SUCCESS_CODE, "获取轨迹成功！", gpsDataWithTruckNoWithTime);
//    }

    /**
     * 绑定设备号
     */
    @PostMapping("bindVehicleDevice")
    @NotNull({"platId", "deviceId", "vehicleNumber"})
    public ReturnData<Object> bindVehicleDevice(String platId, String deviceId, String vehicleNumber) {
        HybReturn<Object> hybReturn = gpsHybService.bindVehicleDevice(platId, deviceId, vehicleNumber);
        return jsonView(hybReturn.getCode(), hybReturn.getMessage());
    }

    /**
     * 设备解绑
     */
    @PostMapping("unBindVehicleDevice")
    @NotNull({"platId", "deviceId", "vehicleNumber"})
    public ReturnData<Object> unBindVehicleDevice(String platId, String deviceId, String vehicleNumber) {
        HybReturn<Object> hybReturn = gpsHybService.unBindVehicleDevice(platId, deviceId, vehicleNumber);
        return jsonView(hybReturn.getCode(), hybReturn.getMessage());
    }

    /**
     * 配置围栏
     */
    @PostMapping("storeDevicePolygon")
    @NotNull({"platId", "transportNo", "vehicleNumber", "driverName", "fenceInfoListJson"})
    public ReturnData<Object> storeDevicePolygon(String platId, String transportNo, String vehicleNumber, String driverName, String fenceInfoListJson) {
        List<FenceInfoDTO> fenceInfoList = JsonUtil.jsonToObject(fenceInfoListJson, new TypeToken<List<FenceInfoDTO>>() {
        });
        HybReturn<List<FenceResult>> hybReturn = gpsHybService.storeDevicePolygon(platId, transportNo, vehicleNumber, driverName, fenceInfoList);
        if (!hybReturn.getSuccess()) return jsonErrView(hybReturn.getMessage());
        for (FenceResult fenceResult : hybReturn.getBody()) {
            if (fenceResult.getResult() == 1) {
                throw new BusinessException(String.format("%S围栏[%s,%s]设置失败：%s", fenceResult.getFenceType() == 1 ? "装货" : "卸货",
                        fenceResult.getLongitude(), fenceResult.getLatitude(), fenceResult.getMessage()));
            }
        }
        return jsonView(hybReturn.getCode(), hybReturn.getMessage());
    }

    /**
     * 取消围栏
     */
    @PostMapping("deleteStoreDevicePolygon")
    @NotNull({"platId", "transportNo"})
    public ReturnData<FenceResult> deleteStoreDevicePolygon(String platId, String transportNo) {
        HybReturn<FenceResult> hybReturn = gpsHybService.deleteStoreDevicePolygon(platId, transportNo);
        if (hybReturn.getCode() == 3002) return jsonView(SUCCESS_CODE, "围栏未配置！");
        if (hybReturn.getCode() == 3022) return jsonView(SUCCESS_CODE, "围栏已结束！");
        return jsonView(hybReturn.getCode(), hybReturn.getMessage(), hybReturn.getBody());
    }

    /**
     * 获取围栏
     */
    @PostMapping("getDevicePolygon")
    @NotNull({"platId", "transportNo"})
    public ReturnData<ShippingFence> getDevicePolygon(String platId, String transportNo) {
        HybReturn<ShippingFence> hybReturn = gpsHybService.getDevicePolygon(platId, transportNo);
        return jsonView(hybReturn.getCode(), hybReturn.getMessage(), hybReturn.getBody());
    }

    /**
     * 接收围栏实时推送
     * 暂时只做通知处理
     */
    @PostMapping("pushDevicePolygon")
    @SkipLoginVerify
    public ReturnData<Object> pushDevicePolygon(ShippingFence shippingFence) {
        HybReturn<Object> hybReturn = gpsHybService.pushDevicePolygon(shippingFence);
        return jsonView(hybReturn.getCode(), hybReturn.getMessage());
    }

    /**
     * 获取轨迹（通过运单号）
     */
    @PostMapping("getTrackByShipping")
    @NotNull({"platId", "transportNo"})
    public ReturnData<List<Location>> getTrackByShipping(String platId, String transportNo) {
        HybReturn<List<DeviceTrack>> trackByShipping = gpsHybService.getTrackByShipping(platId, transportNo);
        if (!trackByShipping.getSuccess()) return jsonErrView(trackByShipping.getMessage());
        List<Location> locationList = gpsHybService.hybTrack2Locations(trackByShipping.getBody());
        return jsonView(SUCCESS_CODE, "获取运单轨迹成功", locationList);
    }

    /**
     * 获取轨迹（通过设备号）
     */
    @PostMapping("getTrackByDeviceId")
    @NotNull({"platId", "deviceId", "startTime", "endTime"})
    public ReturnData<List<Location>> getTrackByDeviceId(String platId, String deviceId, Long startTime, Long endTime) {
        HybReturn<List<DeviceTrack>> trackByShipping = gpsHybService.getTrackByDeviceId(platId, deviceId, startTime, endTime);
        if (!trackByShipping.getSuccess()) return jsonErrView(trackByShipping.getMessage());
        List<Location> locationList = gpsHybService.hybTrack2Locations(trackByShipping.getBody());
        return jsonView(SUCCESS_CODE, "获取车辆轨迹成功", locationList);
    }

    /**
     * 批量获取轨迹（通过设备号）
     */
    @PostMapping("getTracksByDeviceId")
    @NotNull({"platId", "deviceIds", "startTime", "endTime"})
    public ReturnData<Map<String, List<Location>>> getTracksByDeviceId(String platId, String[] deviceIds, Long startTime, Long endTime) {
        List<HybReturn<List<DeviceTrack>>> hybReturn = gpsHybService.getTracksByDeviceId(platId, deviceIds, startTime, endTime);
        Map<String, List<Location>> listMap = new HashMap<>();
        for (HybReturn<List<DeviceTrack>> deviceTracks : hybReturn) {
            if (deviceTracks.getSuccess()) {
                List<Location> locationList = gpsHybService.hybTrack2Locations(deviceTracks.getBody());
                listMap.put(ArrayUtil.first(deviceTracks.getBody()).getDevice(), locationList);
            }
        }
        return jsonView(SUCCESS_CODE, "获取车辆轨迹成功", listMap);
    }

    /**
     * 获取设备信息（通过设备号）
     */
    @PostMapping("getTrackDeviceInfo")
    @NotNull({"platId", "deviceId"})
    public ReturnData<EquipmentInfo> getTrackDeviceInfo(String platId, String deviceId) {
        HybReturn<EquipmentInfo> trackByShipping = gpsHybService.getTrackDeviceInfo(platId, deviceId);
        return jsonView(trackByShipping.getCode(), trackByShipping.getMessage(), trackByShipping.getBody());
    }

    /**
     * 批量获取设备信息（通过设备号）
     */
    @PostMapping("getTrackDeviceInfos")
    @NotNull({"platId", "deviceIds"})
    public ReturnData<List<EquipmentInfo>> getTrackDeviceInfos(String platId, String[] deviceIds) {
        List<HybReturn<EquipmentInfo>> trackDeviceInfos = gpsHybService.getTrackDeviceInfos(platId, deviceIds);
        List<EquipmentInfo> infoList = new ArrayList<>();
        for (HybReturn<EquipmentInfo> trackDeviceInfo : trackDeviceInfos) {
            if (trackDeviceInfo.getSuccess()) infoList.add(trackDeviceInfo.getBody());
        }
        return jsonView(SUCCESS_CODE, "获取成功", infoList);
    }

}
